package com.cloud.school.app.controller;

import com.cloud.school.app.config.security.SecurityUserUtils;
import com.cloud.school.app.model.CollegeModel;
import com.cloud.school.app.model.MajorModel;
import com.cloud.school.app.model.UserModel;
import com.cloud.school.app.service.CollegeService;
import com.cloud.school.app.service.MajorService;
import com.cloud.school.app.service.UserService;
import com.cloud.school.app.util.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName College
 * @Description TODO
 * @Author 36297
 * @Date 2018/12/14 1:03
 * @Version 1.0
 */
@Api(tags = "学校信息控制器")
@RestController
@RequestMapping("/college")
public class CollegeController {

    @Autowired
    private CollegeService collegeService;
    @Autowired
    private MajorService majorService;
    @Autowired
    private UserService userService;

    @ApiOperation("查询所有学校")
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public ResponseUtil<List<CollegeModel>> getAll() {
        return ResponseUtil.ok(collegeService.selectDetailAll());
    }

    @ApiOperation("查询学校所有专业")
    @RequestMapping(value = "/majors", method = RequestMethod.GET)
    public ResponseUtil<List<MajorModel>> getAllMajor(@ApiParam("学校id") @RequestParam Long collegeId) {
        return ResponseUtil.ok(majorService.selectDetailByCollegeId(collegeId));
    }

    @ApiOperation("查询学校专业下的所有用户")
    @RequestMapping(value = "/majorUsers", method = RequestMethod.GET)
    public ResponseUtil<List<UserModel>> majorUsers(@ApiParam("学校id") @RequestParam Long collegeId, @ApiParam("专业id") @RequestParam Long majorId) {
        return ResponseUtil.ok(userService.queryByCollegeIdAndMajorId(collegeId, majorId));
    }

    @ApiOperation("添加学校")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResponseUtil<Boolean> add(@RequestBody CollegeModel collegeModel) {
        collegeModel.setCreateUid(SecurityUserUtils.getCurrentUserId());
        return ResponseUtil.ok(collegeService.save(collegeModel));
    }

    @ApiOperation("添加专业")
    @RequestMapping(value = "/addMajor", method = RequestMethod.POST)
    public ResponseUtil<Boolean> addMajor(@RequestBody MajorModel majorModel) {
        majorModel.setCreateUid(SecurityUserUtils.getCurrentUserId());
        return ResponseUtil.ok(majorService.save(majorModel));
    }

    @ApiOperation("删除学校(会删除专业)")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseUtil<Boolean> delete(@ApiParam("学校id") @RequestParam Long collegeId) {
        return ResponseUtil.ok(collegeService.delete(collegeId));
    }

    @ApiOperation("删除专业")
    @RequestMapping(value = "/deleteMajor", method = RequestMethod.POST)
    public ResponseUtil<Boolean> deleteMajor(@ApiParam("专业id") @RequestParam Long majorId) {
        return ResponseUtil.ok(majorService.delete(majorId));
    }
}
